using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using System.Collections;
using System.Runtime.InteropServices;
using System.ComponentModel;

namespace WindowsGame
{
  class Shot : StaticMapObject
  {
    private bool mRemove;
    private Vector2 mMovement;

    public bool Remove
    {
      get
      {
        return this.mRemove;
      }
    }

    public void Update(TileMap map)
    {
      if ((this.Bounds.Left + this.mMovement.X < 0 ||
           this.Bounds.Left + this.mMovement.X < this.Bounds.Left - Global.ScreenResolution.X) ||

          (this.Bounds.Right + this.mMovement.X > map.MapWidth * map.TileWidth ||
           this.Bounds.Right + this.mMovement.X > this.Bounds.Right + Global.ScreenResolution.X) ||

          (this.Bounds.Top + this.mMovement.Y < 0 ||
           this.Bounds.Top + this.mMovement.Y < this.Bounds.Top - Global.ScreenResolution.Y) ||

          (this.Bounds.Bottom + this.mMovement.Y > map.MapHeight * map.TileHeight ||
           this.Bounds.Bottom + this.mMovement.Y > this.Bounds.Bottom + Global.ScreenResolution.Y))
      {
        this.mRemove = true; 
      }
      else
      {
        if (this.mMovement.X < 0)
        {
          Tile temp = map.checkCollision(this.Bounds, TileMap.DirectionToCheckCollision.left);
          if (temp != null)
          {
            this.mRemove = true;
          }
        }
        else
        {
          this.Bounds = new TileRect(this.Bounds.X + this.mMovement.X, this.Bounds.Y, this.Bounds.Width, this.Bounds.Height);
        }


        if ( this.mMovement.X > 0)
        {
          Tile temp = map.checkCollision(this.Bounds, TileMap.DirectionToCheckCollision.right);
          if (temp != null)
          {
            this.mRemove = true;
          }
        }
        else
        {
          this.Bounds = new TileRect(this.Bounds.X + this.mMovement.X, this.Bounds.Y, this.Bounds.Width, this.Bounds.Height);
        }

        if ( this.mMovement.Y < 0)
        {
          Tile temp = map.checkCollision(this.Bounds, TileMap.DirectionToCheckCollision.up);
          if (temp != null)
          {
            this.mRemove = true;
          }
        }
        else
        {
          this.Bounds = new TileRect(this.Bounds.X, this.Bounds.Y + this.mMovement.Y, this.Bounds.Width, this.Bounds.Height);
        }

        if ( this.mMovement.Y > 0)
        {
          Tile temp = map.checkCollision(this.Bounds, TileMap.DirectionToCheckCollision.down);
          if (temp != null)
          {
            this.mRemove = true;
          }
        }
        else
        {
          this.Bounds = new TileRect(this.Bounds.X, this.Bounds.Y + this.mMovement.Y, this.Bounds.Width, this.Bounds.Height);
        }
      }
    }

    public Shot(Vector2 startPosition, Texture2D texture, Vector2 movement)
    {
      this.Bounds     = new TileRect(startPosition.X, startPosition.Y, this.Bounds.Width, this.Bounds.Height);
      this.Texture    = texture;
      this.mMovement  = movement;
      this.mRemove    = false;
    }
  }
}
